package com.glls.order.controller;

import com.glls.common.R;
import com.glls.common.vo.OrderVo;
import com.glls.order.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author junyang
 * @version 1.0.0
 * @date 2025/2/24 10:40
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;


    @RequestMapping("/tran1")
    public R noTransaction(@RequestBody OrderVo order){

        int result = orderService.addOrderWithNoTransaction(order);
        if(result == 1){
            return R.ok().message("添加订单成功");
        }
        return R.error().message("添加订单失败");
    }



    @RequestMapping("/tran2")
    public R localTransaction(@RequestBody OrderVo order) throws ClassNotFoundException {

        int result = orderService.addOrderWithLocalTransaction(order);
        if(result == 1){
            return R.ok().message("添加订单成功");
        }
        return R.error().message("添加订单失败");
    }


    //预留作业   演示分布式事务场景
    @RequestMapping("/tran3")
    public R distributeTransaction(@RequestBody OrderVo order) {

        int result = orderService.addOrderWithDistributeTransaction(order);
        if(result == 1){
            return R.ok().message("添加订单成功");
        }
        return R.error().message("添加订单失败");
    }


}
